package com.cynteka.cyfoman.core.collection;

/**
 * User: Sergey
 * Date: 29.07.11
 * Time: 12:10
 */
public class LinkType {

    public static final LinkType ONE_TO_ONE = new LinkType(ConnectionLinkType.ONE, ConnectionLinkType.ONE);
    public static final LinkType ONE_TO_MANY = new LinkType(ConnectionLinkType.ONE, ConnectionLinkType.MANY);
    public static final LinkType MANY_TO_ONE = new LinkType(ConnectionLinkType.MANY, ConnectionLinkType.ONE);
    public static final LinkType MANY_TO_MANY = new LinkType(ConnectionLinkType.MANY, ConnectionLinkType.MANY);


    public final ConnectionLinkType source;
    public final int sourceNumber;
    public final ConnectionLinkType destination;
    public final int destinationNumber;

    public LinkType(ConnectionLinkType source, ConnectionLinkType destination) {
        this.source = source;
        sourceNumber = source.number;
        this.destination = destination;
        destinationNumber = destination.number;
    }

    public LinkType(ConnectionLinkType source, int sourceNumber, ConnectionLinkType destination, int destinationNumber) {
        this.source = source;
        this.sourceNumber = sourceNumber;
        this.destination = destination;
        this.destinationNumber = destinationNumber;
    }

    public LinkType(int sourceNumber, int destinationNumber) {
        this.source = ConnectionLinkType.getType(sourceNumber);
        this.sourceNumber = sourceNumber;
        this.destination = ConnectionLinkType.getType(destinationNumber);
        this.destinationNumber = destinationNumber;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LinkType linkType = (LinkType) o;

        if (destinationNumber != linkType.destinationNumber) return false;
        if (sourceNumber != linkType.sourceNumber) return false;
        if (destination != linkType.destination) return false;
        if (source != linkType.source) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = source != null ? source.hashCode() : 0;
        result = 31 * result + sourceNumber;
        result = 31 * result + (destination != null ? destination.hashCode() : 0);
        result = 31 * result + destinationNumber;
        return result;
    }

    @Override
    public String toString() {
        return "LinkType{" + source + "_TO_" + destination +
                ", sourceNumber=" + sourceNumber +
                ", destinationNumber=" + destinationNumber +
                '}';
    }
}
